/*
    覃金波 2020年8月22日17:44:41
    需要通过点击之后，利用路由的push方式来跳转到这个店铺详情页面
*/
import React, { Component } from 'react'
import TopBar from "../../components/foodStore/TopBar"
import StoreMain from "../../components/foodStore/StoreMain/"
import FooterBar from "../../components/foodStore/FooterBar"
import axios from "../../utils/myaxios"
import { connect } from "react-redux"
import { withRouter } from "react-router-dom"

class FoodStore extends Component {
    componentDidMount(){        
        axios.get("/user/getAllShops",{        
            // this.props.match.params.storeId
            shopID:this.props.match.params.storeId
        }).then((res)=>{        
            if(res.code===200){
                             
                for(let i=0;i<res.results.length;i++){
                    if(parseInt(this.props.match.params.storeId) === res.results[i].shopID){                                 
                        this.props.initStoreMsg(res.results[i])
                    }
                }   
            }      
        }).catch((err)=>{
            console.log(err);
            
        })
    }

    render() {         

        return (     
            <div>
                {/* 顶部店铺信息 */}
                <TopBar storeMsg={this.props.storeMsg}></TopBar>
                {/* 点餐 评价 商家导航条*/}
                <StoreMain shopID={this.props.match.params.storeId}></StoreMain>
                {/* 点餐 评价 商家组件，传入子组件完成 */}
                {/* 给接到的this.props.chilred传值 */}
                {React.Children.map(this.props.children, child => {
                    return React.cloneElement(child, {
                        shopID:this.props.match.params.storeId
                    });
                })}
                {/* 底部外卖总价 */}
                <FooterBar></FooterBar>
            </div>
        )
    }
}

const mapStateToProps = (state)=>{
    return {
        storeMsg:state.foodStoreReducer.initStoreMsg,

    }
}

const mapActionToState = (dispatch)=>{
    return {
        
        initStoreMsg(msg){   
                     
            dispatch({
                type:"init",
                payload:msg
            })
        }
    }
}

export default connect(mapStateToProps,mapActionToState)(withRouter(FoodStore))